package com.shed.agif;

import com.gbr.application.action.ExportAction;
import com.gbr.application.model.Capture;
import com.gbr.application.model.Frame;
import com.ng.framework.AbstractPlugin;
import com.ng.framework.model.IModelController;
import com.ng.framework.service.action.IActionListener;
import com.ng.framework.service.action.IActionService;
import com.ng.framework.service.log.ILogService;
import com.ng.framework.service.log.LogLevel;

public class EncoderPlugin extends AbstractPlugin {
    private ILogService           logService;
    private IActionService        actionService;
    private IModelController      modelController;

    private AnimatedGifEncoder    agi;
    private EncoderActionListener encodeActionListener = new EncoderActionListener();

    @Override
    protected void activateComponent() {
        logService.log(getClass(), LogLevel.TRACE, "Activating");
        actionService.addListener(ExportAction.class, encodeActionListener);
    }

    @Override
    protected void deactivateComponent() {
        logService.log(getClass(), LogLevel.TRACE, "Deactivating");
        actionService.removeListener(ExportAction.class, encodeActionListener);
    }

    public final void bindService(ILogService service) {
        logService = service;
    }

    public final void unbindService(ILogService service) {
        logService = null;
    }

    public final void bindService(IActionService service) {
        actionService = service;
    }

    public final void unbindService(IActionService service) {
        actionService = null;
    }

    public final void bindService(IModelController service) {
        modelController = service;
    }

    public final void unbindService(IModelController service) {
        modelController = null;
    }

    private void encode(String file, Capture capture) {
        int width = capture.getRect().width;
        int height = capture.getRect().height;

        agi = new AnimatedGifEncoder();
        agi.setSize(width, height);
        agi.setQuality(10);
        agi.setRepeat(0);
        agi.start(file);

        System.out.print("Processing...");
        for (Frame frame : capture.getFrames()) {
            agi.addFrame(frame.getImage());
            agi.setDelay(frame.getDelay());
        }
        agi.finish();
        System.out.println("Done");
    }

    class EncoderActionListener implements IActionListener<ExportAction> {
        @Override
        public void actionPerformed(final ExportAction action) {
            final Capture capture = modelController.getModel(Capture.class, action.getId());

            if (capture != null) {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        encode(action.getFile(), capture);
                    }
                }).start();
            } else {
                logService.log(getClass(), LogLevel.WARN, "Could not finde capture in ModelController");
            }
        }
    }
}
